package com.bobby.shop.service.impl;

import com.bobby.shop.mapper.ArticleMapper;
import com.bobby.shop.pojo.Article;
import com.bobby.shop.pojo.ArticleExample;
import com.bobby.shop.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Date 2020/12/8 14:23
 * @Created wenjunpei
 */
@Transactional
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public List<Article> findByTypeCode(String typeCode) {
        ArticleExample articleExample = new ArticleExample();
        ArticleExample.Criteria criteria = articleExample.createCriteria();
        criteria.andTypeCodeLike(typeCode +"%");
        List<Article> articleList = articleMapper.selectByExample(articleExample);
        return articleList;
    }


    @Override
    public Article findByArticleId(String articleId) {
        Article article = articleMapper.selectByPrimaryKey(Integer.parseInt(articleId));
        return article;
    }

    @Override
    public List<Article> findByContentAndTypeCode(String content,String typeCode) {
        ArticleExample articleExample = new ArticleExample();
        ArticleExample.Criteria criteria = articleExample.createCriteria();
        criteria.andTypeCodeLike(typeCode + "%");
        List<Article> articleList = articleMapper.selectByExample(articleExample);
        List<Article> targetList = new ArrayList<>();
        for (Article article : articleList) {
            if(article.getTitle().contains(content)) {
                targetList.add(article);
            }else if(article.getSupplier().contains(content)) {
                targetList.add(article);
            }else if(article.getDescription().contains(content)) {
                targetList.add(article);
            }else if(article.getLocality().contains(content)) {
                targetList.add(article);
            }
        }

        return targetList;
    }
}
